package com.example.ui.learn;

import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;

import com.eton.czh.GifView;
import com.example.huashangapp.HSApplication;
import com.example.huashangapp.R;
import com.example.huashangapp.R.color;
import com.example.net.AnalyzingJson;
import com.example.net.NetRequest;

import com.example.ui.learn.util.CurriculumCenter2;
import com.example.ui.learn.util.MyClassroom;
import com.example.ui.learn.util.MyListViewAdapter2;
import com.example.ui.learn.util.MyListViewAdapter3;

import com.example.util.AsyncLoadImage;
import com.example.util.RefreshableListView;
import com.example.util.RefreshableListView.OnRefreshListener;

import android.app.Activity;
import android.app.AlertDialog;

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;

import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;

import android.util.Log;
import android.view.View;

import android.view.View.OnClickListener;
import android.widget.AdapterView;

import android.widget.Button;

import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class CurriculumCenterActivity extends Activity {
	
	
	private Button titleLeftBtn, titleRightBtn;
	private TextView titleText;
	private RefreshableListView mListView;//带下拉刷新的listview
	private Intent myIntent;
	private TextView getIntentText;
	private String strTitle, id, data;
	private ArrayList<CurriculumCenter2> cca_cc = new ArrayList<CurriculumCenter2>();
	private MyListViewAdapter2 cca_adapter;
	private ListeningInfo listenInfo;
	
	AlertDialog infoad;
	AlertDialog.Builder bulider;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.a_cca);
		cca_adapter.type=1;
		myIntent = getIntent();
		strTitle = myIntent.getStringExtra("title");
		id = myIntent.getStringExtra("id");
		bulider = new AlertDialog.Builder(CurriculumCenterActivity.this);
		bulider.setView(View.inflate(CurriculumCenterActivity.this, R.layout.onloading, null))
				.create();
		infoad = bulider.show();
		init();
	}

	private Handler h = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case 1:
				cca_adapter.setCcInfos(cca_cc);			
				cca_adapter.notifyDataSetChanged();
				infoad.dismiss();
				break;			
			case 4:
				infoad.dismiss();				
				break;
			default:
				break;
			}
		};

	};
	Runnable item2_run = new Runnable() {

		@Override
		public void run() {
			data = NetRequest.Request_cca(id);
			JSONObject j;
			try {
				j = new JSONObject(data);
				String result = j.getString("response");
				if (result.equals("true")) {
					cca_cc=AnalyzingJson.getdata_cca(data);
					HSApplication.item1_cca_cc.put(id,cca_cc);
					h.sendEmptyMessage(1);
				} else {
					h.sendEmptyMessage(4);
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
	};
	private GifView mygifView;

	private void init() {
		titleLeftBtn = (Button) findViewById(R.id.layout_title_left_btn);
		titleRightBtn = (Button) findViewById(R.id.layout_title_right_btn);
		titleText = (TextView) findViewById(R.id.layout_title_middle_text);
		getIntentText = (TextView) findViewById(R.id.item01_text_title);
		mListView = (RefreshableListView) findViewById(R.id.item01_listview);
		titleLeftBtn.setText("返回");
		titleText.setText("选课中心");
		titleRightBtn.setText("正在听");
		Resources r = this.getResources(); 
		int btn_h = (int) r.getDimension(R.dimen.ting_left);
		int btn_left=(int)r.getDimension(R.dimen.text_left);
		titleRightBtn.setPadding(btn_left, 0,  btn_h, 0);
		mygifView = (GifView)findViewById(R.id.testView);
		mygifView.setGifImage(R.drawable.ting);
		mygifView.setVisibility(0);
		getIntentText.setText(strTitle);
		cca_adapter = new MyListViewAdapter2(CurriculumCenterActivity.this,
				cca_cc, new AsyncLoadImage());
		mListView.setAdapter(cca_adapter);
		if(HSApplication.item1_cca_cc.size()>0&&HSApplication.item1_cca_cc.get(id)!=null&&HSApplication.item1_cca_cc.get(id).size()>0){
			cca_cc=HSApplication.item1_cca_cc.get(id);
			h.sendEmptyMessage(1);
		}else{
			new Thread(item2_run).start();
		}
		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View v,
					final int position, long arg3) {
				cca_adapter.current=position;
				cca_adapter.notifyDataSetChanged();
				LinearLayout l = (LinearLayout) v.findViewById(R.id.cca_l2);
				final Button btnfalse = (Button) v
						.findViewById(R.id.aci_btnfalse);
				switch (l.getVisibility()) {
				case View.VISIBLE:
					btnfalse.setVisibility(View.GONE);
					l.setVisibility(View.GONE);
					break;
				case View.GONE:
					btnfalse.setVisibility(View.VISIBLE);
					l.setVisibility(View.VISIBLE);
				default:
					break;
				}
				btnfalse.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						Intent i1_l = new Intent();
						i1_l.setClass(CurriculumCenterActivity.this, ListeningActivity.class);
						if(HSApplication.HSlistInfo.getListen_uri()!=null&&!"".equals(HSApplication.HSlistInfo.getListen_uri())){
							if(HSApplication.HSlistInfo.getListen_uri().equals(cca_cc.get(position).getId())){
								listenInfo=HSApplication.HSlistInfo;
								i1_l.putExtra(ListeningActivity.ISLISTENING,true);
							}else{
								listenInfo=new ListeningInfo("", cca_cc.get(position).getThumb(), cca_cc.get(position).getTitle(), cca_cc.get(position).getTitle1(), cca_cc.get(position).getBlId(), 1, cca_cc.get(position).getId(),"",-1,-1,0);
								i1_l.putExtra(ListeningActivity.ISLISTENING,false);
							};
						}else{
						listenInfo=new ListeningInfo("", cca_cc.get(position).getThumb(), cca_cc.get(position).getTitle(), cca_cc.get(position).getTitle1(), cca_cc.get(position).getBlId(), 1, cca_cc.get(position).getId(),"",-1,-1,0);
						i1_l.putExtra(ListeningActivity.ISLISTENING,false);
						}
						Bundle lisinfo=new Bundle();
						lisinfo.putParcelable(ListeningActivity.LISTEN,listenInfo);
						i1_l.putExtra("intro",Boolean.parseBoolean(cca_cc.get(position).getIntro()));
						i1_l.putExtras(lisinfo);
						startActivity(i1_l);
					}
				});								
			}
		});
		mListView.setOnRefreshListener(new OnRefreshListener() {

			@Override
			public void onRefresh(RefreshableListView listView) {
				new NewDataTask().execute();

			}
		});

		titleLeftBtn.setOnClickListener(cca_ocl);
		titleRightBtn.setOnClickListener(cca_ocl);

	}

	private class NewDataTask extends AsyncTask<Void, Void, String> {

		@Override
		protected String doInBackground(Void... params) {
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
			}

			return null;
		}

		@Override
		protected void onPostExecute(String result) {
			new Thread(item2_run).start();
			mListView.completeRefreshing();
			super.onPostExecute(result);
		}

	}

	int pos = -1;;
	private OnClickListener cca_ocl = new OnClickListener() {

		@Override
		public void onClick(View v) {

			switch (v.getId()) {
			case R.id.layout_title_left_btn:
				finish();
				break;
			case R.id.layout_title_right_btn:
				if(HSApplication.HSlistInfo.getListen_uri()==null||"".equals(HSApplication.HSlistInfo.getListen_uri())){
					Toast.makeText(CurriculumCenterActivity.this, "目前您还没有收听过任何课程!",Toast.LENGTH_SHORT).show();
					return;
				}
				Intent i1_l = new Intent();
				i1_l.setClass(CurriculumCenterActivity.this, ListeningActivity.class);
				Bundle lisinfo=new Bundle();
				lisinfo.putParcelable(ListeningActivity.LISTEN,HSApplication.HSlistInfo);
				i1_l.putExtras(lisinfo);
				i1_l.putExtra(ListeningActivity.ISLISTENING,true);
				CurriculumCenterActivity.this.startActivity(i1_l);
				
				break;
			default:
				break;
			}
		}
	};

}
